package com.ybfenpan.service.log;


import com.ybfenpan.model.Member;
import com.ybfenpan.model.log.LogEle;
import com.ybfenpan.model.log.LogMoney;
import com.ybfenpan.model.log.LogMoneyTi;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.core.BaseService;
import com.ybfenpan.utils.ContextUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class LogMoneyTiService extends BaseService {

    @Resource
    private MemberService memberService;
    @Resource
    private LogMoneyTiService logMoneyTiService;
    @Resource
    private LogMoneyService logMoneyService;

    @Resource
    private LogEleService logEleService;

    /**
     * 保存
     */
    public void saveTi(LogMoneyTi logMoneyTi) {

        //新增记录
        logMoneyTiService.saveOrUpdate(logMoneyTi);


        Member member = ContextUtil.getMember();
        member = memberService.getByKey(member.getId());


        Double num = logMoneyTi.getNum();

        Map mapMember = new HashMap();
        mapMember.put("id", member.getId());
        mapMember.put("diyUpdate", "money=money-" + num);
        //更新余额 
        memberService.update(mapMember);


        member = memberService.getByKey(member.getId());
        Double yu = member.getMoney();


        //新增记录
        LogMoney logMoney = new LogMoney();
        logMoney.setMemberId(member.getId());
        logMoney.setMemberLoginName(member.getLoginName());
        logMoney.setTitle("现金积分提现，提现金额：" + logMoneyTi.getNum() + ",手续费：" + logMoneyTi.getFei());
        logMoney.setState(88);
        logMoney.setTotalNum(num);
        logMoney.setNum(-num);
        logMoney.setYu(yu);
        logMoney.setDongNum(0d);
        logMoney.setFaDate(new Date());
        logMoneyService.saveOrUpdate(logMoney);


    }

    /**
     * 取消
     *
     */
    public void updateCancel(Integer id, String caifuNo) {

        //更新状态
        Map mapLogMoneyTi = new HashMap();
        mapLogMoneyTi.put("id", id);
        mapLogMoneyTi.put("state", 30);
        mapLogMoneyTi.put("commitDate", new Date());
        mapLogMoneyTi.put("caifuNo", caifuNo);

        logMoneyTiService.update(mapLogMoneyTi);


        LogMoneyTi logMoneyTi = logMoneyTiService.getByKey(id);
        Member member = memberService.getByKey(logMoneyTi.getMemberId());


        Double num = logMoneyTi.getNum();

        Map mapMember = new HashMap();
        mapMember.put("id", member.getId());
        mapMember.put("diyUpdate", "money=money+" + num);
        //更新余额 
        memberService.update(mapMember);


        member = memberService.getByKey(member.getId());
        Double yu = member.getMoney();


        //新增记录
        LogMoney logMoney = new LogMoney();
        logMoney.setMemberId(member.getId());
        logMoney.setTitle("提现取消，提现金额：" + logMoneyTi.getNum() + ",手续费：" + logMoneyTi.getFei());
        logMoney.setState(88);
        logMoney.setTotalNum(num);
        logMoney.setNum(num);
        logMoney.setYu(yu);
        logMoney.setDongNum(0d);
        logMoney.setFaDate(new Date());
        logMoneyService.saveOrUpdate(logMoney);
    }

}
